package com.example.ai.config;

import com.example.ai.repository.MongoChatMemoryStore;
import dev.langchain4j.community.store.embedding.redis.RedisEmbeddingStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.ClassPathDocumentLoader;
import dev.langchain4j.data.document.parser.apache.poi.ApachePoiDocumentParser;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.stdio.StdioMcpTransport;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.tool.ToolProvider;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@RequiredArgsConstructor
public class CommonConfig {

    private final EmbeddingModel embeddingModel;


    // 定义一个会话记忆对象提供者
    @Bean
    public ChatMemoryProvider chatMemoryProvider(MongoChatMemoryStore mongoChatMemoryStore) {
        ChatMemoryProvider chatMemoryProvider = new ChatMemoryProvider() {
            @Operation
            public ChatMemory get(Object memoryId) {
                MessageWindowChatMemory memory = MessageWindowChatMemory.builder()
                        .id(memoryId)
                        .maxMessages(10)
                        .chatMemoryStore(mongoChatMemoryStore)// 配置会话存储对象(使用MongoDB存储会话记忆)
                        .build();
                return memory;
            }
        };
        return chatMemoryProvider;
    }


    // 构建向量数据库检索对象
    @Bean
    public ContentRetriever contentRetriever(RedisEmbeddingStore redisEmbeddingStore) {
        return EmbeddingStoreContentRetriever
                .builder()
                .embeddingStore(redisEmbeddingStore) // 从哪里检索
                .embeddingModel(embeddingModel) // 配置向量化模型对象
                .minScore(0.5) // 相似度阈值
                .maxResults(3) // 最多返回多少个结果
                .build();
    }

    @Bean
    public ToolProvider toolProvider() {
        McpTransport transport = new StdioMcpTransport.Builder()
                .command(List.of("D:\\develop\\NodeJS\\npx.cmd", "-y", "@baidumap/mcp-server-baidu-map"))
                .environment(Map.of("BAIDU_MAP_API_KEY", "YnJJyFwAHM8R9DgYBgdaIX2HqaUeq41c")) // 替换为你的百度地图 API Key
                .logEvents(true) // only if you want to see the traffic in the log

                .build();

        McpClient mcpClient = new DefaultMcpClient.Builder()
                .transport(transport)
                .build();

        ToolProvider toolProvider = McpToolProvider.builder()
                .mcpClients(List.of(mcpClient))
                .build();

        return toolProvider;
    }




}
